package com.songlanyun.modules.ship.controller.admin;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.alibaba.fastjson.JSONObject;
import com.songlanyun.common.annotation.RepeatSubmit;
import com.songlanyun.common.annotation.SysLog;
import com.songlanyun.common.enums.OrderConstant;
import com.songlanyun.modules.order.entity.Order;
import com.songlanyun.modules.rabbitmq.mq.producer.SendOrderOrRightsOrderMsgService;
import com.songlanyun.modules.ship.entity.OrderPackageExpress;
import com.songlanyun.modules.ship.model.dto.ExpressShipDTO;
import com.songlanyun.modules.user.controller.AbstractController;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.songlanyun.modules.ship.entity.OrderPackage;
import com.songlanyun.modules.ship.service.OrderPackageService;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;



/**
 * 订单的发货包裹
 *
 * @author lubo
 * @email sunlightcs@gmail.com
 * @date 2020-11-19 13:49:33
 */
@RestController
@RequestMapping("admin/order/package")
public class OrderPackageAdminController extends AbstractController {
    @Autowired
    private OrderPackageService orderPackageService;


    @Autowired
    private SendOrderOrRightsOrderMsgService sendOrderOrRightsOrderMsgService;
    /**
     * 商家对物流订单Order的所有商品一起发货
     */
    @SysLog("商家对物流订单Order的所有商品一起发货")
    @RepeatSubmit(value = "5")
    @PostMapping("/express")
    //@RequiresPermissions("ship:saleorderpackage:save")
    public R express(@RequestBody ExpressShipDTO dto){
		OrderPackage orderPackage = orderPackageService.expressShip(getShopId(),dto);
        sendOrderOrRightsOrderMsgService.expressShipSuccessSendMsg(orderPackage);
        return R.ok().put("data",orderPackage);
    }


    /**
     * 商家对物流订单Order的所有商品分包起发货
     */
    @SysLog("商家对物流订单Order的所有商品分包起发货")
    @RepeatSubmit(value = "5")
    @PostMapping("/partexpress")
    //@RequiresPermissions("ship:saleorderpackage:save")
    public R partexpress(@RequestBody ExpressShipDTO dto){
        OrderPackage orderPackage = orderPackageService.partExpressShip(getShopId(),dto);
        sendOrderOrRightsOrderMsgService.expressShipSuccessSendMsg(orderPackage);
        return R.ok().put("data",orderPackage);
    }


    /**
     * 商家查看单个包裹
     */
    @GetMapping("/info/{orderId}/{packageId}")
    //@RequiresPermissions("ship:saleorderpackage:save")
    public R info(@PathVariable("orderId") Long orderId,@PathVariable("packageId") Long packageId ){
        OrderPackage orderPackage = orderPackageService.loadByOrderIdAndPackageId(orderId,packageId,true);
        orderPackage.loadOrderPackageItems();
        if(orderPackage.getGoodsPackageType().equals(OrderConstant.GoodsPackageType.EXPRESS))
        {
            orderPackage.loadExpress();
        }
        else
        {
            orderPackage.loadPickup();
        }
        return R.ok().put("data",orderPackage);
    }


    /**
     * 商家查看订单的所有包裹
     */
    @GetMapping("/allpackage/{orderId}")
    //@RequiresPermissions("ship:saleorderpackage:save")
    public R allPackage(@PathVariable("orderId") Long orderId){
        List<OrderPackage> list = orderPackageService.loadListByOrderId(orderId);
        list.forEach(o->{
            o.loadOrderPackageItems();
            if(o.getGoodsPackageType().equals(OrderConstant.GoodsPackageType.EXPRESS))
            {
                o.loadExpress();
            }
            else
            {
                o.loadPickup();
            }
        });
        return R.ok().put("list",list);
    }



    /**
     * 商家修改物流发货信息
     * @param dto
     * @return
     */
    @SysLog("商家修改物流发货信息")
    @RepeatSubmit(value = "5")
    @PostMapping("/express/update")
    //@RequiresPermissions("ship:saleorderpackage:save")
    public R expressUpdate(@RequestBody ExpressShipDTO dto){
        OrderPackage orderPackage = orderPackageService.expressUpdate(getShopId(),dto);
        return R.ok().put("data",orderPackage);
    }

    /**
     * 商家对自提订单Order的所有商品一起手动备货
     */
    @SysLog("商家对自提订单Order的所有商品一起手动备货")
    @RepeatSubmit(value = "5")
    @PostMapping("/pickup")
    //@RequiresPermissions("ship:saleorderpackage:update")
    public R pickup(@RequestBody ExpressShipDTO dto){
        OrderPackage orderPackage = orderPackageService.pickupShip(getShopId(),dto);
        sendOrderOrRightsOrderMsgService.pickupShipSuccessSendMsg(orderPackage);
        return R.ok().put("data",orderPackage);
    }

    /**
     * 商家后台核销
     */
    @SysLog("商家后台核销")
    @RepeatSubmit(value = "5")
    @PostMapping("/verification")
    //@RequiresPermissions("ship:saleorderpackage:update")
    public R verification(@RequestBody ExpressShipDTO dto){
        Order order = orderPackageService.verification(getShopId(), dto);
        sendOrderOrRightsOrderMsgService.verificationSuccessSendMsg(order);
        return R.ok().put("data",order);
    }

    /**
     * 商家通过核销码查询订单
     */
    @RepeatSubmit(value = "5")
    @PostMapping("/getorder-by-verification")
    //@RequiresPermissions("ship:saleorderpackage:update")
    public R loadOrderByVerification(@RequestBody ExpressShipDTO dto){
        Order order = orderPackageService.loadOrderByVerification(getShopId(), dto);
        order.getChildOrderList(true);
        return R.ok().put("data",order);
    }


    /**
     * 物流跟踪
     */
    @GetMapping("/express/select/{orderId}/{packageId}")
    //@RequiresPermissions("ship:saleorderpackage:save")
    public R expressSelect(@PathVariable("orderId") Long orderId,@PathVariable("packageId") Long packageId){
        OrderPackageExpress orderPackageExpress = orderPackageService.expressSelect(orderId,packageId);

        if(orderPackageExpress==null)
        {
            return R.ok().put("data",null);
        }

        if(StringUtils.isBlank(orderPackageExpress.getQueryResult()))
        {
            return R.ok().put("data",null);
        }

        JSONObject jsonObject = JSONObject.parseObject(orderPackageExpress.getQueryResult());
        return R.ok().put("data",jsonObject);
    }

}
